/*
 * @Author: ✨Fireflx 
 * @Date: 2022-02-16 15:44:18 
 * @Last Modified by: ✨Fireflx
 * @Last Modified time: 2022-02-16 19:49:37
 */

const { Fans } = require('../models')

const follow = async ctx => {
    const { body: comment } = ctx.request;
    await Fans.create(comment)
        .then(res => {
            isCommented = res ? true : false;
            ctx.body = res ? {
                code: 200,
                msg: "关注成功！",
            } : {
                code: 300,
                msg: "关注错误！",
            }
        }).catch(err => {
            ctx.body = {
                code: 500,
                msg: '关注异常！',
            }
        })
}

const unfollow = async ctx => {
    const { username, author } = ctx.request.body;
    await Fans.findOneAndDelete({ username, author })
        .then(res => {
            ctx.body = res ? {
                code: 200,
                msg: "取关成功！",
            } : {
                code: 300,
                msg: "取关错误！",
            }
        }).catch(err => {
            ctx.body = {
                code: 500,
                msg: '取关异常！',
                err,
            }
        })
}

const findByUser = async ctx => {
    //可传入每页几条
    let { page, author } = ctx.query
    //对查询第几页进行兜底
    if (!page || isNaN(Number(page))) {
        page = 1
    } else {
        page = Number(page)
    }
    //每页条数
    const pageSize = 4;
    //计算总页数
    let comCount = 0;
    await Comment.find({ author }).count()
        .then(res => {
            comCount = res
        })
    let pageCount = 0;
    if (comCount > 0) {
        pageCount = Math.ceil(comCount / pageSize)
    }
    //判断当前页码范围
    if (pageCount > 0 && page > pageCount) {
        page = pageCount
    } else if (page < 1) {
        page = 1
    }
    //计算起始位置
    const start = (page - 1) * pageSize;

    await Comment.find({ author }).skip(start).limit(pageSize)
        .then(res => {
            ctx.body = (res && res.length > 0) ? {
                code: 200,
                msg: '查询成功！',
                page,
                pageSize,
                comCount,
                data: res
            } : {
                code: 300,
                msg: "查询失败"
            }
        }).catch(err => {
            ctx.body = {
                code: 500,
                msg: '查询异常！',
                err,
            }
        })
}

const fansList = async ctx => {
    let { page, author } = ctx.query;
    let fansCount = 0,
        pageCount = 0,
        start = 0;
    const pageSize = 4;
    page = Number(page) > 0 ? Number(page) : 1;
    await Fans.find({ author }).count()
        .then(res => {
            fansCount = res;
        })
    pageCount = fansCount ? Math.ceil(fansCount / pageSize) : 0;
    page = (pageCount && pageCount < page) ? pageCount : page;
    start = (page - 1) * pageSize;
    await Fans.find({ author }).skip(start).limit(pageSize)
        .then(res => {
            ctx.body = (res && res.length > 0) ? {
                code: 200,
                msg: '查询成功！',
                page,
                pageSize,
                fansCount,
                data: res
            } : {
                code: 300,
                msg: "查询失败"
            }
        }).catch(err => {
            ctx.body = {
                code: 500,
                msg: '查询异常！',
                err,
            }
        })
}

module.exports = {
    follow,
    unfollow,
    fansList
}